package com.jnu.ilearn;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.android.material.navigation.NavigationView;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.jnu.ilearn.bean.DataBank;
import com.jnu.ilearn.bean.PointRecord;
import com.jnu.ilearn.bean.User;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private DrawerLayout drawerLayout;
    private NavigationView navigationView;
    ImageView menu;
    private String[] tabTitles=new String[2];
    private List<Fragment> mDatas=new ArrayList<>();
    private User user_1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getSupportActionBar().hide();

        //将登录的用户信息传进来
        Intent intent=getIntent();
        user_1=(User) intent.getSerializableExtra("User");

        drawerLayout = (DrawerLayout) findViewById(R.id.activity_na);
        navigationView = (NavigationView) findViewById(R.id.nav);
        menu= (ImageView) findViewById(R.id.main_menu);
        View headerView = navigationView.getHeaderView(0);//获取头布局
        drawerLayout.setFitsSystemWindows(true);
        drawerLayout.setClipToPadding(false);
        if(user_1.getIdentity().equals("student")){
            navigationView.inflateMenu(R.menu.student_menu);
            ImageView imageView=headerView.findViewById(R.id.person);
            imageView.setImageResource(R.drawable.student);
        }
        else
        {
            navigationView.inflateMenu(R.menu.manager_menu);
            ImageView imageView=headerView.findViewById(R.id.person);
            imageView.setImageResource(R.drawable.manager);
        }
        menu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (drawerLayout.isDrawerOpen(navigationView)){
                    drawerLayout.closeDrawer(navigationView);
                }else{
                    drawerLayout.openDrawer(navigationView);
                }

            }
        });
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                //item.setChecked(true);
                switch (item.getItemId()){
                    case R.id.ChangePassword:
                        Intent intent=new Intent(MainActivity.this,ChangePassword.class);
                        startActivity(intent);
                         break;
                    case R.id.quit:
                        intent=new Intent(MainActivity.this,LoginActivity.class);
                        DataBank dataBank=new DataBank(MainActivity.this);
                        dataBank.loadData();
                        User user=dataBank.getUser();
                        user.setAccount("account");
                        dataBank.saveData();
                        startActivity(intent);
                        break;
                    case R.id.total_occupy:
                        intent=new Intent(MainActivity.this,Occupied_seatActivity.class);
                        startActivity(intent);
                        break;
                    case R.id.PointStore:
                        intent=new Intent(MainActivity.this,PointStore.class);
                        intent.putExtra("Point",user_1.getPoint());
                        intent.putExtra("Account",user_1.getAccount());
                        startActivity(intent);
                        break;
                    case R.id.personal_center:
                        intent=new Intent(MainActivity.this,personal_center.class);
                        startActivity(intent);


                }
                drawerLayout.closeDrawer(navigationView);
                return true;
            }
        });

        TextView userName=headerView.findViewById(R.id.user_name);
        userName.setText(user_1.getAlias());
        tabTitles= new String[]{"教室1","教室2"};
        mDatas.add(FirstFragment.newInstance());
        mDatas.add(SecondFragment.newInstance());

        ViewPager2 viewPagerFragments=findViewById(R.id.viewPager2_content);
        viewPagerFragments.setAdapter(new MyFragmentAdapter(this));

        TabLayout tabLayoutHeader=findViewById(R.id.tabLayout_header);
        TabLayoutMediator tabLayoutMediator=new TabLayoutMediator(tabLayoutHeader, viewPagerFragments, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                tab.setText(tabTitles[position]);
            }
        });
        tabLayoutMediator.attach();

        TextView tip=findViewById(R.id.tip);
        tip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MainActivity.this, TipActivity.class);
                startActivity(intent);
            }
        });
    }
    public boolean onMenuOpened(int featureId, Menu menu)
    {
        if (menu != null) {
            if (menu.getClass().getSimpleName().equalsIgnoreCase("MenuBuilder")) {
                try {
                    Method method = menu.getClass().getDeclaredMethod("setOptionalIconsVisible", Boolean.TYPE);
                    method.setAccessible(true);
                    method.invoke(menu, true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        onDestroy();
        return super.onMenuOpened(featureId, menu);
    }

    private class MyFragmentAdapter extends FragmentStateAdapter {

        public MyFragmentAdapter(@NonNull FragmentActivity fragmentActivity) {
            super(fragmentActivity);
        }

        @NonNull
        @Override
        public Fragment createFragment(int position) {
            return mDatas.get(position);
        }

        @Override
        public int getItemCount() {
            return 2;
        }
    }
}